package com.gupao.netty.bytebuf;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import io.netty.buffer.ByteBufUtil;

public class NettyByteBuf {

    public static void main(String[] args) {
        // jvm堆内存
        ByteBuf byteBuf = ByteBufAllocator.DEFAULT.heapBuffer();
        // 写数据
        byteBuf.markWriterIndex();
        byteBuf.writeBytes(new byte[]{1,2,3,4});

        printMsg(byteBuf);
        byteBuf.resetWriterIndex();
        byteBuf.writeInt(6);
        printMsg(byteBuf);

        // 读
        byteBuf.markReaderIndex();
        byte a = byteBuf.readByte();
        byte b = byteBuf.readByte();
        byteBuf.resetReaderIndex();
        printMsg(byteBuf);



    }

    public static void printMsg(ByteBuf buf){
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("read index:").append(buf.readerIndex());
        stringBuilder.append("write index:").append(buf.writerIndex());
        stringBuilder.append("capacity:").append(buf.capacity());
        ByteBufUtil.appendPrettyHexDump(stringBuilder,buf);

        System.out.println(stringBuilder.toString());
    }
}
